﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LunwenYun.Portal.WebLogic;
using LunwenYun.Infrastructure.Entities.Enum;
using LunwenYun.Portal.Biz;
using LunwenYun.Infrastructure.Entities.Entity;

namespace LunwenYun.Portal.Web.Controllers
{
    [Compress]
    [ErrorHandle]
    [Auth(UserRole.Admin)]
    public class adminController : SiteBaseController
    {
        //
        // GET: /admin/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 约稿人申请
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult editorApply(int page=0)
        {
            var apply = SystemBiz.GetAllEditorApply(page,15);
            ViewBag.ApplySheet = new ItemListBox<ApplySheet>(apply)
                .BuildPage(SystemBiz.GetAllEditorApplyForPage(), page, 15,
                new PageParameter()
                {
                    Style = "home"
                });
            return View();
        }
        public ActionResult applydetail(int id)
        {
            ViewBag.ApplySheet = new ApplySheet().Find(id.ToString())
                .FindPeriodical()
                .FindUser();
            return View();
        }
        public int agreeApply(int id)
        {
            if (SystemBiz.AgreeEditorApply(id) != null)
            {
                return 1;
            }
            return 0;
        }
        public int refuseApply(int id)
        {
            if (SystemBiz.RefuseEditorApply(id) != null)
            {
                return 1;
            }
            return 0;
        }
        /// <summary>
        /// 新期刊发布审核
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult newPeriodicalApply(int page=0)
        {
            var newPeriodical = SystemBiz.GetAllNewPeriodicalApply(page, 15);
            ViewBag.Periodicals = new ItemListBox<Periodical>(newPeriodical)
                .BuildPage(SystemBiz.GetAllNewPeriodicalApplyForPage(), page, 15,
                new PageParameter()
                {
                    Style = "home"
                });
            return View();
        }
        public int passtheperiodical(int id)
        {
            if (SystemBiz.PutNewPeriodical(id) != null)
            {
                return 1;
            }
            return 0;
        }
        public int deletetheperiodical(int id)
        {
            if (SystemBiz.DeleteNewPeriodical(id) != null)
            {
                return 1;
            }
            return 0;
        }
        public ActionResult industrycs(int org = -1, int page = 0)
        {
            ViewBag.Org = SystemBiz.GetIndustryOrganization();
            List<IndustryCS> data = SystemBiz.GetIndustryCS(org,false, page, 15);
            ViewBag.CS = new ItemListBox<IndustryCS>(data).BuildPage(
                SystemBiz.GetIndustryCSCount(org,false), page, 15,
                new PageParameter() { Style = "home" });
            return View();
        }
        public ActionResult csoption(int t, string u)
        {
            SystemBiz.CsOption(t, u);
            return RedirectToAction("industrycs");
        }
        public ActionResult newindustrycs()
        {
            ViewBag.Org = SystemBiz.GetIndustryOrganization();
            return View();
        }
        [HttpPost]
        public ActionResult newindustrycs(User user,string IndustryName,string IndustryCode,int Organization,string Title)
        {
            user.Birthday = DateTime.MinValue;
            user.Email = user.UserName;
            user.RcdDate = DateTime.Now;
            user.Gender = Gender.Female;
            user.IsActivate = true;
            user.IsAuth = false;
            user.IsComplete = false;
            user.IsRefuse = false;
            user.SystemRole = UserRole.PaperAgent;
            user.Industry = Organization;
            UserBiz.Register(user, true);

            var cs = new IndustryCS()
            {
                CreateTime = DateTime.Now,
                Enable = true,
                Industry = IndustryName,
                Organization = Organization,
                Phone = user.Phone,
                QQ = user.QQ,
                Title = Title,
                UserName = user.UserName
            };
            SystemBiz.newIndustryCS(cs);
            return RedirectToAction("industrycs");
        }
    }
}
